#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define FILE_PATH "send.mp3"
#define BUFF_SIZE 1024

int main()
{
    FILE *file = fopen(FILE_PATH, "rb");
    if (NULL == file)
    {
        printf("Open file %s error! Press any key to exit.\n", FILE_PATH);
        exit(0);
    }

    int sock_serv = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in serv_addr;
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    serv_addr.sin_port = htons(1234);
    bind(sock_serv, (struct sockaddr*)&serv_addr, sizeof(serv_addr));

    listen(sock_serv, 20);

    struct sockaddr_in clnt_addr;
    socklen_t clntLen = sizeof(clnt_addr);
    int sock_clnt = accept(sock_serv, (struct sockaddr*)&clnt_addr, &clntLen);

    char buffer[BUFF_SIZE] = {0};
    int sendCount;
    while ((sendCount = fread(buffer, 1, BUFF_SIZE, file)) > 0)
    {
        send(sock_clnt, buffer, sendCount, 0);
    }

    shutdown(sock_clnt, SHUT_WR);
    recv(sock_clnt, buffer, BUFF_SIZE, 0);

    fclose(file);
    close(sock_clnt);
    close(sock_serv);

    return 0;
}